package com.smsc.headend.task.engine.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
public class ReportTimeUtils {
    public static List<Date> cutDate(String dateType, Long startTv, Long endTv, int interval) {
        try {
            Date dBegin = new Date(startTv*1000);
            Date dEnd = new Date(endTv*1000);
            return findDates(dateType, dBegin, dEnd, interval);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static List<Date> findDates(String dateType, Date dBegin, Date dEnd, int interval) throws Exception {
        List<Date> listDate = new ArrayList<>();
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(dEnd);
        listDate.add(dBegin);
        while (calEnd.after(calBegin)) {
            switch (dateType) {
                case "M":
                    calBegin.add(Calendar.MONTH, interval);
                    break;
                case "D":
                    calBegin.add(Calendar.DAY_OF_YEAR, interval);break;
                case "H":
                    calBegin.add(Calendar.HOUR, interval);break;
                default:
                    log.info("no implementation currently");
            }
            if (calEnd.after(calBegin))
                listDate.add(new Date(calBegin.getTimeInMillis()));
            else
                listDate.add(new Date(calEnd.getTimeInMillis()));
        }
        return listDate;
    }
}
